/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.core.security.tags;

import org.jsecurity.web.tags.PermissionTag;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A tag that renders it's body content when any of it's permission is satisfied.
 * <p/>
 * Eg.
 * <code>
 * <sample:hasAnyPermission name="[domain.action1.*][domain.action2.*]">
 *   ...
 * </sample:hasAnyPermission>
 * </code>
 * <p/>
 * The content will be rendered if either "domain.action1.*" or "domain.action2.*"
 * permissions are satisfied.
 *
 * @author tmjee
 * @version $Date: 2009-06-27 15:56:30 +0000 (Sat, 27 Jun 2009) $ $Id$
 */
public class HasAnyPermissionTag extends PermissionTag {


    private Pattern pattern;

    public HasAnyPermissionTag() {
        pattern = Pattern.compile("\\[([^\\[\\]]*)\\]");                
    }


    protected boolean showTagBody(String p) {
        Matcher matcher = pattern.matcher(p);
        while(matcher.find()) {
            String permission = matcher.group(1);
            boolean permitted = getSubject().isPermitted(permission);
            if (permitted) {
                return true;
            }
        }
        return false;
    }
}
